#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void notice(int signo)
{
    std::cout << "get a signal: " << signo << " pid: " << getpid() << std::endl;
    while(true)
    {
        pid_t rid = waitpid(-1, nullptr, WNOHANG);
        if(rid > 0)
        {
            std::cout << "wait child success, rid: " << rid << std::endl;
        }
        else
        {
            std::cout << "wait child done" << std::endl;
            break;
        }

    }
}

void DoOtherThing()
{
    std::cout << "DoOtherThing~" << std::endl;
}

int main()
{
    signal(SIGCHLD, notice);

    pid_t id = fork();
    if(id == 0)
    {
        std::cout << "I am child process, pid : " << getpid() << std::endl;
        sleep(1);
        exit(1);
    }

    while (true)
    {
        DoOtherThing();
        sleep(1);
    }

    return 0;
}